<template>
  <div>
    <div style="padding:0 35px 0 35px;">

      <el-form :model="queryCondition" ref="refQueryCondition" label-width="auto" style="width:100%;" size="small">
        <el-row>
<!--          <el-col span="8">
            <el-form-item label="项目名称">
              <el-input v-model="project.name" disabled></el-input>
            </el-form-item>
          </el-col>-->
<!--          <el-col :span="8">
            <el-form-item label="评价名称:" prop="name">
              <el-input v-model="queryCondition.name" autocomplete="off" prefix-icon="el-icon-search"
                        placeholder="请输入评价名称" clearable />
            </el-form-item>
          </el-col>-->
<!--          <el-col :span="7">
            <el-button type="primary" style="margin-left:10px;" @click="loadDataList()" size="small">查询</el-button>
            &lt;!&ndash;            <el-button type="success" size="small" :disabled="!this.dataLoaded.project"&ndash;&gt;
            &lt;!&ndash;              @click="openCreateDlg">新增评价</el-button>&ndash;&gt;
          </el-col>-->
          <!--        <el-col span="12">-->
          <!--        <el-form-item label="建设单位">-->
          <!--          <el-input v-model="project.name" readonly></el-input>-->
          <!--        </el-form-item>-->
          <!--        </el-col>-->
<!--          <el-col span="8">-->
<!--            <el-form-item label="项目类型">-->
<!--              <el-input v-model="projectTypeMap[project.type]" readonly></el-input>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
          <!--        <el-col span="8">-->
          <!--        <el-form-item label="项目级别">-->
          <!--          <el-input v-model="project.level" readonly></el-input>-->
          <!--        </el-form-item>-->
          <!--        </el-col>-->
        </el-row>
        <el-row>


          <!--        <el-col :span="6">-->
          <!--          <el-form-item label="评价日期:" prop="type">-->
          <!--            <el-date-picker-->
          <!--                v-model="queryCondition.pjrq" style="width:304px"-->
          <!--                type="date"-->
          <!--                placeholder="选择日期">-->
          <!--            </el-date-picker>-->
          <!--          </el-form-item>-->
          <!--        </el-col>-->
          <!--        <el-col :span="6">-->
          <!--          <el-form-item label="内部收益率:" prop="state">-->
          <!--            <el-input v-model="queryCondition.irr.bg" style="width:140px"  placeholder=""/> &nbsp;~ &nbsp;<el-input v-model="queryCondition.irr.ed"  style="width:140px"  placeholder=""/>&nbsp; %-->
          <!--          </el-form-item>-->
          <!--        </el-col>-->


        </el-row>
      </el-form>
      <el-table border highlight-current-row  v-loading="dataListLoading"
        :data="dataList" :key="'compute_version_list'" row-key="id" tooltip-effect="dark" style="width: 100%"
        :header-cell-style="{background:'#f5f7fa','text-align':'center'}"  >

        <el-table-column fixed width="60" type="index" label='序号'   align="left" >
          <template slot-scope="scope">
            {{scope.row.index}}
          </template>
<!--          <template slot-scope="scope">-->
<!--          <div >{{indexList[scope.$index]}}</div>-->
<!--          </template>-->
        </el-table-column>
        <el-table-column fixed prop="name" show-overflow-tooltip label="名称" align="left" />
<!--        <el-table-column prop="modelId" show-overflow-tooltip  label="评价模型" align="left">-->
<!--          <template slot-scope="item">{{modelMap[item.row.modelId]}}</template>-->
<!--        </el-table-column>-->
        <!--                                      <el-table-column prop="buildYearCount" label="建设期年限" align="left" />-->
        <el-table-column prop="calculateYearStart" width="65" label="评价期" align="center">
          <template slot-scope="item">
            {{ item.row.calculateYearStart?(item.row.calculateYearEnd - item.row.calculateYearStart+1+'年'):'' }}
          </template>
        </el-table-column>
<!--        <el-table-column prop="evaPriceDollar" show-overflow-tooltip width="80" label="评价油价 ($/bbl)" align="center">-->
<!--        </el-table-column>-->
        <el-table-column prop="constructionInvest" width="80" label="建设投资  (万元)" align="center">
        </el-table-column>
        <el-table-column label="内部收益率(%)" align="center">
          <el-table-column prop="irrBeforeTax" width="80" label="税前" show-overflow-tooltip align="center" />
          <el-table-column prop="irrAfterTax" width="80" label="税后" show-overflow-tooltip align="center" />
        </el-table-column>
        <el-table-column label="投资回收期(年)" align="center">
          <el-table-column prop="pbpBeforeTax" width="80" label="税前" show-overflow-tooltip align="center" />
          <el-table-column prop="pbpAfterTax" width="80" label="税后" show-overflow-tooltip align="center" />
        </el-table-column>
        <el-table-column label="财务净现值(万元)" align="center">
          <el-table-column prop="npvBeforeTax" width="80" label="税前" show-overflow-tooltip align="center" />
          <el-table-column prop="npvAfterTax" width="80" label="税后" show-overflow-tooltip align="center" />
        </el-table-column>
        <el-table-column prop="createTime" width="160" label="评价时间" align="center" />
        <el-table-column prop="createTime" width="65" label="类型" align="center">
          <template slot-scope="item">
            <span :style="'color:'+(item.row.type=='temp'?'red':'green')">
            {{computeVersionTypeMap[item.row.type]}}
              </span>
          </template>
        </el-table-column>
<!--        <el-table-column prop="remark" width="90" label="备注" align="center" />-->
        <!--      <el-table-column prop="createUser" width="80" label="评价人" align="center"/>-->
        <el-table-column key="option" align="center" width="150" fixed="right" label="操作">
          <template slot-scope="item">
            <div style="float:left;">
              <el-button  size="mini" @click="openPage('/evaluation/compute-invoke',project.id,{version:item.row.id,src:$route.fullPath})">详情</el-button>
              <el-button type="danger"  size="mini" v-if="checkPermission('compute_version:delete')" @click="del(item.row.id)">删除</el-button>
<!--            <el-button size="mini" v-if="project.state=='report'&&item.row.type!='back'&&!item.row.parent&&item.row.type!='temp'&&item.row.name!='项目'"-->
<!--              type="primary" @click="report(item.row)">上报</el-button>-->

<!--&lt;!&ndash;            <el-button v-if="(project.type=='eor'||project.type=='productivity_construction_old')&&item.row.mark=='result'" size="mini" @click="openPage('/evaluation/compute-subtract',project.id,{version:item.row.id})">详情</el-button>&ndash;&gt;-->
<!--            <el-button v-if="project.extraEvaluateMethod=='subtract'&&item.row.mark=='result'&&item.row.name!='项目'" size="mini" @click="openPage('/evaluation/compute-subtract',project.id,{version:item.row.id,src:$route.fullPath})">详情</el-button>-->
<!--              <el-button v-if="(!item.row.parentId)&&item.row.name!='项目'" size="mini" @click="openEditDlg(item.row)">编辑</el-button>-->
<!--              <template v-if="!item.row.parentId">-->
<!--&lt;!&ndash;                <el-divider direction="vertical"></el-divider>&ndash;&gt;-->
<!--                <el-button size="mini" @click="openCopyDlg(item.row)">复制</el-button>-->
            </div>
          </template>


        </el-table-column>
      </el-table>
<!--      <el-pagination style="text-align:center;padding-top:10px;" :current-page="pageNo" background :page-size="pageSize"-->
<!--        layout="prev, pager, next" :total="dataCount" @current-change="loadDataList"></el-pagination>-->
      <el-pagination
          style="text-align:center;padding-top:10px;"
          background
          @size-change="handlePageSizeChange"
          @current-change="loadDataList"
          :current-page="pageNo"
          :page-sizes="[10, 20, 30, 40,50,100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="dataCount">
      </el-pagination>
      <el-dialog v-loading="(computeVersion.editMode&&loadingComputeVersion)||isLoading" :title="computeVersion.editMode?'更新信息':'新建'"
        :visible.sync="detailDlgVisible" :close-on-click-modal="false" width="70%">
        <el-form label-width="auto" style="width:100%;margin-top:20px;" size="medium">
          <el-form-item label="评价名称:" >
            <el-input v-model="computeVersion.name"  disabled="true" autocomplete="off" placeholder="请输入内容"></el-input>
          </el-form-item>
          <el-form-item label="评价模型:">
            <el-select v-model="computeVersion.modelId" placeholder="请选择" :disabled="computeVersion.editMode">
              <el-option v-for="item in modelList" :key="item.id" :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-row>
            <el-col span="6">
              <el-form-item label="评价期:">
                <el-input v-model="computeVersion.evaluationPeriod" autocomplete="off" @input="evaluationPeriodChange"
                  placeholder="请输入内容" :disabled="computeVersion.editMode"> <template slot="append">年</template></el-input>
              </el-form-item>
            </el-col>
            <el-col span="6">
              <el-form-item label="开始年:">
                <el-input v-model="computeVersion.calculateYearStart" autocomplete="off" @input="calculateYearStartChange" placeholder="开始年" :disabled="computeVersion.editMode"> </el-input>
              </el-form-item>
            </el-col>
            <el-col span="6">
              <el-form-item label="结束年:">
                <el-input v-model="computeVersion.calculateYearEnd" autocomplete="off" disabled placeholder="结束年">
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
<!--          <el-row span="8">-->
<!--            <el-form-item label="成本取值标准:">-->
<!--              <data-select :data="costStandardVersionList" value-key="year"-->
<!--                           v-model="computeVersion.costStandardYear" autocomplete="off" placeholder="请输入内容"-->
<!--                           :disabled="computeVersion.editMode"></data-select>-->
<!--            </el-form-item>-->
<!--          </el-row>-->
          <el-row>
            <el-form-item label="">
              <el-checkbox label="根据经济极限年判断评价期" v-model="computeVersion.cutoff" :true-label="1" :false-label="0"
                style="margin-right: 15px" :disabled="computeVersion.editMode">
              </el-checkbox>
            </el-form-item>
          </el-row>
          <el-form-item label="备注:">
            <el-input type="textarea" v-model="computeVersion.remark" autocomplete="off" placeholder="请输入内容"></el-input>
          </el-form-item>

        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button type="success" :disabled="isLoading" @click="save">确定</el-button>
          <el-button @click="detailDlgVisible = false">取消</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>
<script>
const moment = require('moment');
  export default {
    name: "ComputeVersionList",
    $$route:{
      meta: {
        title: '评价列表'
      }
    },
    data() {
      return {
        isLoading:false,
        dataListLoading: false,
        maxVersionNum: 0,//序号数组
        queryCondition: {
          name: '',
        },
        project: {},
        pageNo: 1,
        pageSize: 20,
        companyList: [],
        companyMap: {},
        detailDlgVisible: false,
        computeVersion: {},
        dataList: [],
        modelList: [],
        modelMap: {},
        dataCount: 1,
        loadingComputeVersion: false,
        projectTypeMap: {},
        computeVersionTypeMap: {},
        dataLoaded: {
          project: false,
        },
        costStandardVersionList:[],
      }
    },
    computed: {},
    created() {
      this.project.id = this.$route.query.id;
      this.loadProjectInfo(this.project.id);
      this.loadDataList();
     // this.loadCostStandardVersion();
      this.loadMetaNameMap('product-type', (map) => {
        this.projectTypeMap = map;
      });
      this.loadMetaNameMap('compute-version-type', (map) => {
        this.computeVersionTypeMap = map;
      });
      this.$emit('top-menu', '/project/info-list');
    },
    watch: {},
    methods: {
      loadDataList(pageNo) {
        this.dataListLoading = true; 
        if (!pageNo) {
          pageNo = 1;
        } else {
          this.pageNo = pageNo;
        }
        this.queryCondition.pageNo = this.pageNo;
        this.queryCondition.pageSize = this.pageSize;
        this.queryCondition.projectId = this.project.id;

        this.$get("/compute_version/data", this.queryCondition, (response) => {
          let list = response.data;
          let index1 = 1;
          for(let item of list) {
            item.index = index1++;
            this.fixedData(item);
            let projectItemList = [];
            // let index2 = 1;
            if(item.children&&item.children.length>0) {
                let prefix = item.index + "."
              for(let child of item.children) {
                this.fixedData(child);
                if (item.extraEvaluateMethod=='subtract') {
                    projectItemList.push(child);
                    child.index = prefix+'1.'+ projectItemList.length;
                }

              }
            }
          }
          this.dataList = list;
          this.dataCount = response.total;
        }, () => {
          this.dataListLoading = false;
        });
      },
      fixedData(item){
        if(item.irrBeforeTax) {
          item.irrBeforeTax = item.irrBeforeTax.toFixed(2);
        }
        if(item.irrAfterTax) {
          item.irrAfterTax = item.irrAfterTax.toFixed(2);
        }
        if(item.npvBeforeTax) {
          item.npvBeforeTax = item.npvBeforeTax.toFixed(0);
        }
        if(item.npvAfterTax) {
          item.npvAfterTax = item.npvAfterTax.toFixed(0);
        }
        if(item.pbpBeforeTax) {
          item.pbpBeforeTax = item.pbpBeforeTax.toFixed(1);
        }
        if(item.pbpAfterTax) {
          item.pbpAfterTax = item.pbpAfterTax.toFixed(1);
        }
      },
      del(id) {
        this.$confirm('确定删除该条数据?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.isLoading = true;
          this.$post("/compute_version/delete?id=" + id, {}, (data) => {
            this.$message.success('删除成功！');
            this.loadDataList();
          }, (response) => {
            this.isLoading = false;
          });
        }).catch(() => {});
      },
      openEditDlg(item) {
        this.detailDlgVisible = true;
        this.loadingComputeVersion = true;
        this.$get("/compute_version/info/" + item.id, {}, (response) => {
          let computeVersionData = response.data;
          this.computeVersion = computeVersionData;
          this.computeVersion.editMode = true;
        }, () => {
          this.loadingComputeVersion = false;
        });

        // this.computeVersion = JSON.parse(JSON.stringify(item));

      },

      openCreateDlg() {
        this.detailDlgVisible = true;
        //let date = new Date();
        //let timeStr = date.getFullYear() + '-' + date.getMonth() + '-' + date.getDay();
        this.$get("/compute_version/maxNum/"+ this.project.id,{}, (res) => {
          let maxVersionNum = 0;
          if (res.data) {
            maxVersionNum = res.data;
          }
          let newVersionNum = 1+maxVersionNum;
          let data = {
            name:  "评价版本V"+ newVersionNum,
            versionNum : newVersionNum
          };
          this.$set(data, 'cutoff', 1);
          if (this.modelList && this.modelList.length > 0) {
            data.modelId = this.modelList[0].id;
          }

          if(this.project.extraEvaluateMethod=='subtract'){
            //老区项目、提高采收率项目使用对减法
            data.extraEvaluateMethod='subtract';
          }


          if (this.project.year && this.project.year > 0) {
            data.calculateYearStart = this.project.year;
          }
          this.computeVersion = data;
        })

      },
      save(id) {
        const message = this.$message;
        this.computeVersion.projectId = this.project.id;
        this.isLoading = true;
        if (this.computeVersion.copyMode) {
          this.computeVersion.type = 'default';
          this.$post("/compute_version/copy", this.computeVersion, (response) => {
            // this.computeVersion.id = response.data;
            this.detailDlgVisible = false;
            this.$message.success('复制成功！');
            this.loadDataList();
          },()=>{
            this.isLoading = false;
          });
        } else if (this.computeVersion.editMode) {
          this.$post("/compute_version/update", this.computeVersion, (response) => {
            this.detailDlgVisible = false;
            message.success('更新成功！');

            this.loadDataList();
          },()=>{
            this.isLoading = false;
          });
        } else {
          this.$post("/compute_version/create", this.computeVersion, (response) => {
            this.detailDlgVisible = false;
            if(!this.computeVersion.calculateYearStart||!this.computeVersion.calculateYearEnd) {
              this.isLoading = false;
              this.$message.warning("评价起始、终止年份不能为空");
              return;
            }
            if(!this.computeVersion.evaluationPeriod) {
              this.isLoading = false;
              this.$message.warning("评价期不能为空");
              return;
            }
            message.success('创建成功！');
            this.loadDataList();
            // this.openPage('/evaluation/compute-invoke', this.project.id, {
            //   version: response.data
            // })
            if(this.project.extraEvaluateMethod=='subtract'){
              this.openPage('/evaluation/compute-subtract',this.project.id,{version:response.data,src:this.$route.fullPath});
            } else {
              this.openPage('/evaluation/compute-invoke',this.project.id,{version:response.data,src:this.$route.fullPath});
            }
          },()=>{
            this.isLoading = false;
          });
        }
      },
      loadModelList: function () {
        this.$get("/settings/model/publish/data", {targetType:'office'
          ,targetId:this.project.buildOffice
          ,productType:this.project.productType}, (response) => {
          this.modelList = response.data;
          let map = {};
          for (let i = 0; i < this.modelList.length; i++) {
            map[this.modelList[i].id] = this.modelList[i].name;
          }
          this.modelMap = map;
        });
      },
      loadProjectInfo: function (id) {
        this.$get("/project_info/info/" + id, {}, (response) => {
          this.project = response.data;
          this.loadModelList();
        }, () => {
          this.dataLoaded.project = true;
        });
      },
      report: function (item) {
        if(!item.computeTime) {
          this.$message.warning("该版本未进行计算")
          return;
        }
        let data = {};
        data.computeVersion = item.id;
        // this.$post("/compute_version_state/report", item, (response) => {
        this.$post("/project/report_version/create", data, (response) => {
          this.$message.success("上报成功")
          this.loadProjectInfo(this.project.id);
          this.loadDataList(this.pageNo);
        });
      },
 
      evaluationPeriodChange(val) {
        if(this.computeVersion.calculateYearStart>0)
        {
            this.computeVersion.calculateYearEnd=  parseInt(this.computeVersion.calculateYearStart) +  parseInt(val)-1;
        }
      },

      calculateYearStartChange(val) {
        if(this.computeVersion.evaluationPeriod>0)
        {
            this.computeVersion.calculateYearEnd= parseInt(this.computeVersion.evaluationPeriod) +  parseInt(val) -1;
        }
      },
      /**
       * 加载成本取值发布版本
       * @param dataList
       */
      // loadCostStandardVersion() {
      //   this.$get("/cost_standard_version/data?state=publish", {
      //   }, (response) => {
      //     this.costStandardVersionList = response.data;
      //   }, (response) => {
      //   });
      //
      // },
      handlePageSizeChange(size){
        this.pageSize = size;
        this.loadDataList(this.pageNo);
      },
      openCopyDlg(item) {
        this.detailDlgVisible = true;
        this.loadingComputeVersion = true;
        this.$get("/compute_version/info/" + item.id, {}, (response) => {
          let computeVersionData = response.data;
          this.computeVersion = computeVersionData;
          this.computeVersion.evaluationPeriod = computeVersionData.calculateYearEnd-computeVersionData.calculateYearStart+1;
          this.computeVersion.editMode = true;
          this.computeVersion.copyMode = true;
        }, () => {
          this.loadingComputeVersion = false;
        });

        // this.computeVersion = JSON.parse(JSON.stringify(item));

      },
    },

  }
</script>